﻿#include "SaveFrame.h"
#include"MyFrame.h"
#include<fstream>
#include<iostream>


SaveFrame::SaveFrame(const wxString& title, wxPanel* parent)
	: wxFrame(NULL, wxID_ANY, title, wxPoint(375, 600), wxSize(300, 200)) {
	m_parent = parent;
	m_savePanel = new wxPanel(this, wxID_ANY);
	m_saveNameText = new wxTextCtrl(m_savePanel, 100, "输入元器件名", wxPoint(90, 70), wxDefaultSize);
	m_saveButton = new wxButton(m_savePanel, 101, "保存", wxPoint(110, 100), wxDefaultSize);

	Connect(ID_SAVEBUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(SaveFrame::Save));


}

void SaveFrame::Save(wxCommandEvent& WXUNUSED(event)) {

    wxString textName = m_saveNameText->GetValue();
    Json::Value name;
    name["name"] = textName.ToStdString();

    wxFileDialog m_savePath(nullptr, "Save File", "", textName, "JSON files (*.json)|*.json"
        , wxFD_SAVE | wxFD_OVERWRITE_PROMPT);

    if (m_savePath.ShowModal() == wxID_CANCEL) {
        return;
    }
    wxString selectedPath = m_savePath.GetPath();

    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    std::vector<Json::Value>& draws = myframe->m_br->draws;
    Json::Value drawing;

    // 计算并保存每个部分的起始点到组合图形中心的 x 和 y 坐标
    double totalX = 0.0;
    double totalY = 0.0;
    double partX = 0.0;
    double partY = 0.0;

    for (auto& part : draws) {
        // 假设每个部分都有 x 和 y 属性
        if (part["type"].asString() == "roud") {
            // 圆形
            partX = 2 * part["centerx"].asInt();
            partY = 2 * part["centery"].asInt();
        }
        else if (part["type"].asString() == "rect") {
            // 矩形
            partX = 2 * part["startx"].asInt() + part["width"].asInt();
            partY = 2 * part["starty"].asInt() + part["height"].asInt();

        }
        else if (part["type"].asString() == "line") {
            // 直线
            partX = part["startx"].asInt() + part["endx"].asInt();
            partY = part["starty"].asInt() + part["endy"].asInt();

        }
        else if (part["type"].asString() == "arc") {
            
            partX = 2 * part["startx"].asInt() + part["width"].asInt();
            partY = 2 * part["starty"].asInt() + part["height"].asInt();

        }

        totalX += partX;
        totalY += partY;

    }

    // 计算组合图形中心的坐标
    double centerX = totalX / draws.size() / 2;
    double centerY = totalY / draws.size() / 2;
    // 添加组合图形中心坐标到 name 中
    name["centerX"] = centerX;
    name["centerY"] = centerY;

    // 遍历每个部分并调整起始点
    for (auto& part : draws) {
        if (part["type"].asString() == "roud") {
            // 圆形
            part["centerx"] = part["centerx"].asDouble() - centerX;
            part["centery"] = part["centery"].asDouble() - centerY;
        }
        else if (part["type"].asString() == "rect") {
            // 矩形
            part["startx"] = part["startx"].asDouble() - centerX;
            part["starty"] = part["starty"].asDouble() - centerY;

        }
        else if (part["type"].asString() == "line") {
            // 直线
            part["startx"] = part["startx"].asDouble() - centerX;
            part["starty"] = part["starty"].asDouble() - centerY;

            part["endx"] = part["endx"].asDouble() - centerX;
            part["endy"] = part["endy"].asDouble() - centerY;

        }
        else if (part["type"].asString() == "arc") {
            
            part["startx"] = part["startx"].asDouble() - centerX;
            part["starty"] = part["starty"].asDouble() - centerY;

        }
        else if (part["type"].asString() == "point") {
            // 圆形
            part["centerx"] = part["centerx"].asDouble() - centerX;
            part["centery"] = part["centery"].asDouble() - centerY;
        }
    }

    drawing.append(name);
    for (auto i : draws){ 
        drawing.append(i); 
    }
        

    std::ofstream jsonFile(selectedPath.ToStdString());
    jsonFile << drawing;
    jsonFile.close();

    Close();
}

    

    //wxString msg = wxString::Format("Mouse position:%d, %d", centerX, totalX);
    //wxMessageBox(msg, "Information", wxOK | wxICON_INFORMATION);
    
    
  /*// 添加组合图形中心坐标到 name 中
    name["centerX"] = centerX;
    name["centerY"] = centerY;

    // 添加图形数据到成员变量中
    drawing.append(name);
    int n = drawing.size();*/ 

    
   